package com.leadnews.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leadnews.mapper.LevelMapper;
import com.leadnews.model.levelT;
import com.leadnews.pojo.Level;
import com.leadnews.vo.ResponseResult;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.leadnews.utils.AppJwtUtil.getClaimsBody;

@RestController
@RequestMapping
@CrossOrigin(origins = "*",maxAge = 3600)
public class LevelController {
    @PostMapping("/selectFirst")
    public ResponseResult created(@RequestBody Map<String, String> map) {
        String token = map.get("token");
        ResponseResult<Level> result = new ResponseResult();
        result.setCode(0);
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody(token);
//        int i = AppJwtUtil.verifyToken(claimsBody);
//        if(i<1){
//
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        System.out.println("解析token成功 ==> 用户的id值 == " + id);
        LambdaQueryWrapper<Level> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Level::getUserId, id);
        Level level = levelMapper.selectOne(queryWrapper);
        if (level != null) {
            result.setData(level);
            result.setCode(200);
        } else {
            result.setErrorMessage("token过期");
        }
        return result;
    }

    @PutMapping("/getTime")
    public ResponseResult sign(@RequestBody Map<String, String> map) {
        String token = map.get("token");
        ResponseResult result = new ResponseResult();
        result.setCode(0);
        Object id = null;
        levelT levelT = new levelT();
        int index = 0;
        try {
            Claims claimsBody = getClaimsBody(token);
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        int i = 0;
        System.out.println("解析token成功 ==> 用户的id值 == " + id);
        LambdaQueryWrapper<Level> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Level::getUserId, id);
        Level level = levelMapper.selectOne(queryWrapper);
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//创建一个日期格式.
        String format1 = format.format(date);
        String[] split = format1.split("-");
        String newDate = split[0] + split[1] + split[2];
        System.out.println(Integer.parseInt(newDate));
        if (level != null) {
            if (level.getExperience().equals("0")) {
                level.setClickTime(date);
                level.setExperience("10");
                level.setDesignation("初出茅庐");
                level.setAddlevel("10");
                level.setStar("1");
                level.setNextlevel("20");
                int i1 = levelMapper.updateById(level);
                if (i1 > 0) {
                    result.setData(level);
                    result.setCode(200);
                    result.setErrorMessage("签到成功，恭喜你踏入初出茅庐");
                }
                return result;
            }
            String experience = level.getExperience();
            Long exper = (long) Integer.parseInt(experience);
            System.out.println(exper);
            Date clickTime = level.getClickTime();
            String format2 = format.format(clickTime);
            String[] date2 = format2.split("-");
            String newDate1 = date2[0] + date2[1] + date2[2];
            System.out.println(Integer.parseInt(newDate1));
            if (Integer.parseInt(newDate) > Integer.parseInt(newDate1)) {
                level.setClickTime(date);
                List<Long> listExperience = levelT.listlEVEL();
                loop:
                for (i = 0; i < listExperience.size() - 1; i++) {
                    if (exper >= listExperience.get(i) && exper < listExperience.get(i + 1)) {
                        index++;
                        break loop;
                    }
                }
                if (index == 0) {
                    i = 8;
                }
                System.out.println(i);
                List<Long> experience1 = levelT.listExperience();
                Long aLong = experience1.get(i);
                Long newExper = (exper + aLong);
                level.setExperience(newExper + "");

                System.out.println("用户经验为：" + (exper + aLong));
                List<String> name = levelT.listName();

                if (newExper >= listExperience.get(i + 1)) {
                    if ((i + 1) >= 9) {

                    } else {
                        Long aLong1 = listExperience.get(i + 2);
                        level.setNextlevel(newExper* 100 / aLong1  + "");
                        level.setStar((i + 2) +"");
                        String s = name.get(i + 1);
                        level.setDesignation(s);
                        level.setAddlevel(experience1.get(i + 1) + "");
                        result.setErrorMessage("签到成功，经验" + "+" + experience1.get(i) + "恭喜您晋升" + s + ",武道漫长，再接再厉");
                    }
                } else {
                    Long aLong1 = listExperience.get(i + 1);
                    level.setNextlevel(newExper * 100/ aLong1  + "");
                    level.setStar((i + 1) +"");
                    String s = name.get(i);
                    level.setDesignation(s);
                    level.setAddlevel(experience1.get(i) + "");
                    result.setErrorMessage("签到成功，经验" + "+" + experience1.get(i) + ",武道漫长，再接再厉");
                }

                System.out.println(level);
                int i1 = levelMapper.updateById(level);
                if (i1 > 0) {
                    result.setData(level);
                    result.setCode(200);

                }

            } else {
                result.setErrorMessage("今日已签到，请明日再来");
            }
        }
        return result;
    }

    @PostMapping("/level/created")
    public ResponseResult created1(@RequestBody Map<String, String> map) {
        String token = map.get("token");
        ResponseResult<Level> result = new ResponseResult();
        result.setCode(0);
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody(token);
//        int i = AppJwtUtil.verifyToken(claimsBody);
//        if(i<1){
//
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        System.out.println("解析token成功 ==> 用户的id值 == " + id);
        LambdaQueryWrapper<Level> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Level::getUserId, id);
        Level level = levelMapper.selectOne(queryWrapper);
        if (level != null) {
            result.setData(level);
            result.setCode(200);
        } else {
            result.setErrorMessage("用户无等级");
        }
        return result;
    }

    @Autowired
    LevelMapper levelMapper;
}
